package cn.stylefeng.guns.modular.yingbao.service.impl;

import cn.stylefeng.guns.base.pojo.page.LayuiPageFactory;
import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.yingbao.entity.SysGroupSetting;
import cn.stylefeng.guns.modular.yingbao.mapper.SysGroupSettingMapper;
import cn.stylefeng.guns.modular.yingbao.model.params.SysGroupSettingParam;
import cn.stylefeng.guns.modular.yingbao.model.result.SysGroupSettingResult;
import  cn.stylefeng.guns.modular.yingbao.service.SysGroupSettingService;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 团队收益设置表 服务实现类
 * </p>
 *
 * @author jj
 * @since 2019-12-18
 */
@Service
public class SysGroupSettingServiceImpl extends ServiceImpl<SysGroupSettingMapper, SysGroupSetting> implements SysGroupSettingService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseData add(SysGroupSettingParam param){
        SysGroupSetting grade = this.baseMapper.selectOne(new QueryWrapper<SysGroupSetting>().eq("grade", param.getGrade()));
        if(grade != null){
            return ResponseData.error(500,"该等级已存在");
        }
        SysGroupSetting entity = getEntity(param);
        boolean save = this.save(entity);
        if(save){
            return ResponseData.success();
        }
        return ResponseData.error(500,"添加失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(SysGroupSettingParam param){
        this.removeById(getKey(param));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SysGroupSettingParam param){
        SysGroupSetting oldEntity = getOldEntity(param);
        SysGroupSetting newEntity = getEntity(param);
        ToolUtil.copyProperties(newEntity, oldEntity);
        this.updateById(newEntity);
    }

    @Override
    public SysGroupSettingResult findBySpec(SysGroupSettingParam param){
        return null;
    }

    @Override
    public List<SysGroupSettingResult> findListBySpec(SysGroupSettingParam param){
        return null;
    }

    @Override
    public LayuiPageInfo findPageBySpec(SysGroupSettingParam param){
        Page pageContext = getPageContext();
        IPage page = this.baseMapper.customPageList(pageContext, param);
        return LayuiPageFactory.createPageInfo(page);
    }

    private Serializable getKey(SysGroupSettingParam param){
        return param.getId();
    }

    private Page getPageContext() {
        return LayuiPageFactory.defaultPage();
    }

    private SysGroupSetting getOldEntity(SysGroupSettingParam param) {
        return this.getById(getKey(param));
    }

    private SysGroupSetting getEntity(SysGroupSettingParam param) {
        SysGroupSetting entity = new SysGroupSetting();
        ToolUtil.copyProperties(param, entity);
        return entity;
    }

}
